﻿using mozhi.smarterp.Finance.Accounts;
using mozhi.smarterp.Finance.Accounts.Repository;
using mozhi.smarterp.Finance.Enums;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;

namespace mozhi.smarterp.Finance.Ieps.Import
{
    public class AccountBalanceImportHandler : ImportHandlerBase<AccountBalanceDto>
    {
        private IAccountAppService _accountAppService;
        private IAccountDimensionRepository _dimensionRepository;
        private List<AccountBalanceDto> _oldBalances;

        public AccountBalanceImportHandler(IServiceProvider serviceProvider) : base(serviceProvider)
        {
            _accountAppService = serviceProvider.GetService(typeof(IAccountAppService)) as IAccountAppService;
            _dimensionRepository = serviceProvider.GetService(typeof(IAccountDimensionRepository)) as IAccountDimensionRepository;
        }

        public override async Task<ImportResultDto> HandleImportData(Guid fileId)
        {

            var accountBalances = (await GetImportData(ModuleCodeType.InitialBalance, fileId)) as List<AccountBalanceDto>;

            var result = new ImportResultDto();

            result.Total = accountBalances.Count;

            result.Errors = new List<string>();

            //for (int i = 0; i < accounts.Count; i++)
            //{
            //    var account = accounts[i];
            //    try
            //    {
            //        if (_oldBalances.Exists(x => x.Code == account.Code))
            //            await UpdateAccount(account);
            //        else
            //            await AddAccount(account);

            //        result.SuccessfulCount += 1;
            //    }
            //    catch (UserFriendlyException ex)
            //    {
            //        result.FailureCount += 1;
            //        result.Errors.Add($"第{i + 1}行有错误：{ex.Message}");
            //    }
            //}

            return result;
        }

        protected override List<AccountBalanceDto> ConvertToData(DataSet ds)
        {
            var result = new List<AccountBalanceDto>();

            //var dt = ds.Tables[0];

            //if (dt.Rows.Count == 0)
            //    return result;

            //foreach (DataRow row in dt.Rows)
            //{
            //    var account = new AccountDto();
            //    account.Code = row["Code"].ToString();
            //    account.Name = row["Name"].ToString();
            //    account.AccountType = GetAccountType(row["AccountType"].ToString());
            //    account.Direction = GetAccountDirectionType(row["Direction"].ToString());
            //    account.IsForeignCurrency = GetBooleanValue(row["IsForeignCurrency"].ToString());
            //    account.IsFundAccount = GetBooleanValue(row["IsFundAccount"].ToString());

            //    var requiredDimensionItems = GetAccountDimensionItems(row["RequiredDimensionNames"].ToString());

            //    requiredDimensionItems.ForEach(item => { item.IsRequire = true; });

            //    var optionDimensionItems = GetAccountDimensionItems(row["OptionDimensionNames"].ToString());

            //    account.DimensionItems = new List<AccountDimensionItemDto>();

            //    account.DimensionItems.AddRange(requiredDimensionItems);
            //    account.DimensionItems.AddRange(optionDimensionItems);

            //    account.ParentId = GetAccountParentId(account.Code);
            //    account.Id = GetAccountId(account.Code);

            //    result.Add(account);
            //}

            return result;
        }
    }
}
